﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WorldOnFire
{
    public class CBattleship : ABCEnemy
    {
        private float[] m_fMissileTimer;
        private int[] m_nMissileCount;

        public override void Initialize(int _nObjectCount)
        {
            // Initialize arrays
            m_fMissileTimer = new float[_nObjectCount];
            m_nMissileCount = new int[_nObjectCount];

            // Initialize base members
            base.Initialize(_nObjectCount);

            // Initialize the members
            m_fOrbitAngleLock = 0.15f;

            for (int iObject = 0; iObject < _nObjectCount; ++iObject)
            {
                m_fMass = 100.0f;
                m_fEngineForce = 7.5f * m_fMass;
                m_fMaximumSpeed = 10.0f;
                m_fTurnSpeed = 0.1f * (float)Math.PI;

                m_fMinimumRange = 300.0f;
                m_fMaximumRange = 500.0f;
                m_fShotDelayPrimary = 10.0f;
                m_fShotDelaySecondary = 2.5f;
                m_fShotSpeed = 50.0f;
                m_fDamage = 10.0f;
                m_fAreaOfEffect = 0.0f;
                m_fMaximumHealth = 500.0f;

                // TODO: SET TARGET INDEX
                m_nTargetIndex[iObject] = 0;
                m_fDisabledTimer[iObject] = 0.0f;
                m_fShotTimerPrimary[iObject] = 0.0f;

                // TODO: LOLWUT
                this.SetAIState(iObject, eAIState.CLOSING);
            }
        }

        public override void LoadContent()
        {
            m_eModelID = CRenderingManager.eModelID.ENEMY_BATTLESHIP;

            base.LoadContent();
        }

        public override void Update(float _fDeltaTime)
        {
            // Loop through each missile battery
            for (int iObject = 0; iObject < m_nObjectCount; ++iObject)
            {
                // Decrement the missile timer
                m_fMissileTimer[iObject] -= _fDeltaTime;

                // If missiles need to be fired
                if (m_nMissileCount[iObject] > 0 && m_fMissileTimer[iObject] < 0.0f)
                {
                    // Fire a missile
                    s_ObjectManager.GetProjectile(ABCProjectile.eProjectileType.MISSILE)
                        .CreateProjectile(this, iObject, new Vector3(-5.75f, -4.0f, 11.0f));
                    s_ObjectManager.GetProjectile(ABCProjectile.eProjectileType.MISSILE)
                        .CreateProjectile(this, iObject, new Vector3(5.75f, -4.0f, 11.0f));

                    // Reset the missile timer and decrement the missiles remaining
                    m_fMissileTimer[iObject] = 0.15f;
                    --m_nMissileCount[iObject];
                }
            }

            // Update base members
            base.Update(_fDeltaTime);
        }

        protected override void FirePrimary(int _nIndex)
        {
            // Set the missile count
            m_nMissileCount[_nIndex] = 8;
        }

        protected override void FireSecondary(int _nIndex)
        {
            s_ObjectManager.GetProjectile(ABCProjectile.eProjectileType.PLASMA)
                .CreateProjectile(this, _nIndex, new Vector3(-12.5f, 0.0f, 0.0f));
            s_ObjectManager.GetProjectile(ABCProjectile.eProjectileType.PLASMA)
                .CreateProjectile(this, _nIndex, new Vector3(12.5f, 0.0f, 0.0f));
        }
    }
}
